/*
* @Author: obsidian
* @Date:   2016-03-01 10:39:43
* @Last Modified by:   obsidian
* @Last Modified time: 2016-04-22 09:11:54
*/

#include "menu_select.h"
#include "../lcd_global.h"
#include "../lcd.h"
#include "control/car.h"
#include "module/pid/pid.h"
#include "module/GPS/GPS.h"
#include "module/steer/steer.h"
#include "module/brake/brake.h"
#include "control/GPSpath/GPSpath.h"
#include "driver/sd/file.h"


static void menu_select_init(void *parg);

static void menu_select_input(void *parg)
{
    car.save();
    steer.read_from_sd();
    steer.init();
    //gps.init();
    gpspath.read_from_sd();
    gpspath.init();
    brake.read_from_sd();
    brake.init();

    pid.init();
}
void menu_save_exec(void* parg)
{
    unsigned char k = 0;
    for(k = 1;k < MAX_CHILD_NUM;k ++)
    {
        if(control_n.child[k] == 0){break;}
        if(control_n.child[k]->save != 0x00)
        {
            control_n.child[k]->save();
        }
    }
}

void menu_clear_exec(void* parg)
{
    car.strjoint("pid");
    f_unlink((const TCHAR *)car.path);
    car.strjoint("gps");
    f_unlink(car.path);
    car.strjoint("encoder");
    f_unlink(car.path);
    car.strjoint("gyro");
    f_unlink(car.path);
    car.strjoint("steer");
    f_unlink(car.path);
    car.strjoint("select");
    f_unlink(car.path);
    car.strjoint("gpspath");
    f_unlink(car.path);
}
signed short f = -30;
struct Item select_i[MAX_ITEM_NUM] = 
{
    //ctx               id  flag   isint color int float pos
    {.ctx = "car_id",   .id = 1,  .ctx_len = 6,  .flag = FILE,  .value_type = INTEGER, .p_value = (void *)&car.version, .func_enter = nop, input, menu_select_input,    nop},
    {.ctx = "save",     .id = 2,  .ctx_len = 5,  .flag = FILE,  .value_type = NONE, .p_value = 0,                       .func_enter = nop, input,  nop,    nop},    
    {.ctx = "clear",    .id = 3,  .ctx_len = 5,  .flag = FILE,  .value_type = NONE, .p_value = 0,                       .func_enter = nop, menu_clear_exec,  nop,    nop},    
    
    //{"setselect",  0,  0,  {0, 0},  menu_set_select,},
};

struct Node select_n = 
{
//     .itemnum        =   sizeof(select_i) / sizeof(select_i[0]),
//     .page_num       =   (unsigned short)sizeof(select_i) / sizeof(select_i[0])/PAGE_SIZE,
    .page_index     =   0,
    .node_id        =   7,
    .p_item         =   select_i,
    .position       =   {0, 0},
    .parent         =   &control_n,
    .child          =   {0},
    .init           =   menu_select_init
};

static void menu_select_init(void *parg)
{
    unsigned char i = 0;
    for(i = 0;i < MAX_ITEM_NUM;i ++)
    {
        if(select_i[i].ctx[0] == 0){break;}
        select_n.itemnum ++;
    }
    select_n.page_num = select_n.itemnum / PAGE_SIZE;

    select_n.save = car.save;
}
